package com.mazaiting.manager.controller;


import com.mazaiting.common.core.ex.exceptions.BaseException;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.manager.domain.form.PermForm;
import com.mazaiting.manager.domain.query.PermPageQuery;
import com.mazaiting.manager.domain.vo.permission.PermDetailVO;
import com.mazaiting.manager.domain.vo.permission.PermPageVO;
import com.mazaiting.manager.service.ISysPermissionService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import com.mazaiting.web.auth.security.annotation.PrePermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 权限控制器
 *
 * @author mazaiting
 * @since 2022-09-08
 */
@Slf4j
@RestController
@RequestMapping("manager/v1/permissions")
@RequiredArgsConstructor
public class SysPermissionController {

    private final ISysPermissionService sysPermissionService;

    /**
     * 权限分页列表
     * @param queryParams 权限查询条件
     * @return 分页数据
     */
    @PrePermission("sys:perms:query")
    @GetMapping("/page")
    public BasePageVO<PermPageVO> listPermPages(PermPageQuery queryParams) {
        return sysPermissionService.listPermPages(queryParams);
    }

    /**
     * 权限详情
     * @param permissionId 权限 ID
     * @return 权限详情
     */
    @PrePermission("sys:perms:update")
    @GetMapping("/{permissionId}")
    public PermDetailVO getPermissionDetail(@PathVariable Long permissionId) throws EmptyObjectException {
        return sysPermissionService.getDetailById(permissionId);
    }

    /**
     * 新增权限
     * @param permForm 权限
     * @return true: 新增成功; false: 新增失败
     */
    @PrePermission("sys:perms:add")
    @PostMapping
    public boolean add(PermForm permForm) throws BusinessException {
        return sysPermissionService.save(permForm);
    }

    /**
     * 修改权限
     * @param permForm 权限
     * @return true: 修改成功; false: 修改失败
     */
    @PrePermission("sys:perms:update")
    @PutMapping
    public boolean update(PermForm permForm) throws BaseException {
        return sysPermissionService.update(permForm);
    }

    /**
     * 删除权限
     * @param ids 权限ID，多个以英文逗号(,)分割
     * @return true: 删除成功; false: 删除失败
     */
    @PrePermission("sys:perms:delete")
    @DeleteMapping("/{ids}")
    public boolean deleteByIds(@PathVariable String ids) {
        return sysPermissionService.deleteByIds(ids);
    }


}
